/**
 * @Title: OauthService.java
 * @author 7828wc
 * @date 2015年9月9日 下午9:08:33
 */
package com.onstar.cn.api;

import com.gm.onstar.sdk.errors.RemoteApiAuthHandlingError;
import com.onstar.cn.api.framework.BaseApiService;
import com.onstar.cn.api.framework.ClazzInjectUtils;
import com.onstar.cn.api.framework.annotation.Path;
import com.onstar.cn.api.framework.annotation.RequestMapping;
import com.onstar.cn.api.util.AuthUtils;
import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.network.entity.RequestEntity;

import java.security.InvalidKeyException;
import java.security.SignatureException;

/**
 * @author 7828wc
 * @ClassName: OauthService.java
 * @Description: Auth service
 * @date 2015年9月9日
 */
@Path("/api/v1/oauth")
public class OauthService extends BaseApiService {
    private String serverUrl;

    /**
     *
     */
    public OauthService(String serverUrl) {
        super(serverUrl);
        this.serverUrl = serverUrl;
    }

    /**
     * @param @param  requestEntity
     * @param @param  username
     * @param @param  password
     * @param @param  clientId
     * @param @param  deviceId
     * @param @param  clientSecret
     * @param @param  scope
     * @param @return
     * @param @throws InvalidKeyException
     * @param @throws SignatureException    设定文件
     * @return RequestEntity    返回类型
     * @throws
     * @Title: autoToken
     * @Description: 不需要Authorization
     */
    @RequestMapping("/token")
    public RequestEntity autoToken(RequestEntity requestEntity, String username, String password, String clientId,
                                   String deviceId, String clientSecret, String scope) throws InvalidKeyException, SignatureException {
        String jwtToken = AuthUtils.encodeForInitialAuth(username, password, clientId, deviceId, clientSecret, scope);
        return getURLRequestEntity("autoToken", jwtToken, requestEntity, username, password, clientId,
                deviceId, clientSecret, scope);
    }

    @RequestMapping("/token")
    public RequestEntity deleteToken(RequestEntity requestEntity,String isRevokeIdToken) {
        if (requestEntity == null) {
            requestEntity = new RequestEntity();
        }
        String url = ClazzInjectUtils.injectUrl(getClass(), "deleteToken", requestEntity,isRevokeIdToken);
        LogManager.getInstance().d("Request url: " + serverUrl + url);
        requestEntity.setUrl(serverUrl + url +"?revokeIdToken=" + isRevokeIdToken);
        return requestEntity;
    }

    /**
     * 不需要Authorization,重新生成Access-token
     *
     * @param requestEntity
     * @param clientId
     * @param idToken
     * @param deviceId
     * @param clientSecret
     * @param scope
     * @return
     * @throws InvalidKeyException
     * @throws SignatureException
     * @throws RemoteApiAuthHandlingError
     */
    @RequestMapping("/token")
    public RequestEntity reauthenticTokenForNOPinCommand(RequestEntity requestEntity, String clientId, String idToken, String deviceId,
                                                         String clientSecret, String scope) throws InvalidKeyException, SignatureException, RemoteApiAuthHandlingError {
        if (requestEntity == null) {
            requestEntity = new RequestEntity();
        }
        String url = ClazzInjectUtils.injectUrl(getClass(), "reauthenticTokenForNOPinCommand", requestEntity, clientId, idToken, deviceId,
                clientSecret, scope);
        requestEntity.setUrl(serverUrl + url);
        String jwtToken = AuthUtils.encodeForReauthentication(clientId, idToken, deviceId, clientSecret, scope);
        requestEntity.setBody(jwtToken);
        return requestEntity;
    }

    /**
     * @param @param  requestEntity
     * @param @param  clientId
     * @param @param  idToken
     * @param @param  deviceId
     * @param @param  clientSecret
     * @param @param  pin
     * @param @param  scope
     * @param @return
     * @param @throws InvalidKeyException
     * @param @throws RemoteApiAuthHandlingError
     * @param @throws SignatureException    设定文件
     * @return RequestEntity    返回类型
     * @throws
     * @Title: authPinToken
     * @Description: 不需要Authorization, PIN码重新生成
     */
    @RequestMapping("/token")
    public RequestEntity authPinToken(RequestEntity requestEntity, String clientId, String idToken, String deviceId,
                                      String clientSecret, String pin, String scope)
            throws InvalidKeyException, RemoteApiAuthHandlingError, SignatureException {
        if (requestEntity == null) {
            requestEntity = new RequestEntity();
        }
        String url = ClazzInjectUtils.injectUrl(getClass(), "authPinToken", requestEntity, clientId, idToken, deviceId,
                clientSecret, pin, scope);
        requestEntity.setUrl(serverUrl + url);
        String jwtToken = AuthUtils.encodeForPinReauthentication(clientId, idToken, deviceId, clientSecret, pin, scope);
        requestEntity.setBody(jwtToken);
        return requestEntity;
    }

    /**
     * @param @param  requestEntity
     * @param @param  username
     * @param @param  password
     * @param @param  clientId
     * @param @param  deviceId
     * @param @param  clientSecret
     * @param @param  scope
     * @param @return
     * @param @throws InvalidKeyException
     * @param @throws SignatureException    设定文件
     * @return RequestEntity    返回类型
     * @throws
     * @Title: upgrade
     * @Description: 需要Authorization, 15-29分钟调用此接口申请新的accessToken
     */
    @RequestMapping("/token/upgrade")
    public RequestEntity upgrade(RequestEntity requestEntity, String clientId, String deviceId,
                                 String clientSecret, String pin)
            throws InvalidKeyException, RemoteApiAuthHandlingError, SignatureException {
        if (requestEntity == null) {
            requestEntity = new RequestEntity();
        }
        String url = ClazzInjectUtils.injectUrl(getClass(), "upgrade", requestEntity, clientId, deviceId, clientSecret, pin);
        requestEntity.setUrl(serverUrl + url);
        String jwtToken = AuthUtils.encodeForUpgradeToken(clientId, deviceId, clientSecret, pin);
        requestEntity.setBody(jwtToken);
        return requestEntity;
    }

}
